package de.bomzhi.mz.pflection.model;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import com.powerflasher.fdt.core.outermodel.IOuterModelElement;

public class IndexOfModelSets<T extends IOuterModelElement> {
	private final List<SetOfStrongTypedModels<T>> listOfSets;
	private final String description;
	private BigDecimal avg;
	private BigDecimal sum;

	public IndexOfModelSets(List<SetOfStrongTypedModels<T>> listOfSets,
			String description) {
		this.listOfSets = listOfSets;
		this.description = description;
	}

	public String getDescription() {
		computeValues();
		return "SUM: " + sum + " AVG: " + avg + " " + description;
	}

	private void computeValues() {
		double sum = 0.0;
		double count = 0.0;
		for (int i = 0; i < listOfSets.size(); i++) {
			sum += i * listOfSets.get(i).getSize();
			count += listOfSets.get(i).getSize();
		}
		if (count == 0.0) {
			avg = BigDecimal.ZERO;
			return;
		}
		this.sum = BigDecimal.valueOf(sum);
		avg = BigDecimal.valueOf(sum / count).setScale(2, RoundingMode.UP);
	}

	public Object[] getModels() {
		List<SetOfStrongTypedModels<T>> result = new ArrayList<SetOfStrongTypedModels<T>>();
		for (SetOfStrongTypedModels<T> setOfStrongTypedModels : listOfSets) {
			if (setOfStrongTypedModels.getSize() > 0) {
				result.add(setOfStrongTypedModels);
			}
		}
		return result.toArray();
	}
}
